<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2013 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

function theme_widget_index_tab_goods(&$setting,&$render){
    $_return = false;

    switch ($setting['selector']) {
    case 'filter':
        parse_str($setting['goods_filter'],$goodsFilter);
        $goodsFilter['goodsOrderBy'] = $setting['goods_order_by'];
        $goodsFilter['goodsNum'] = $setting['limit'];
        $_return = b2c_widgets::load('Goods')->getGoodsList($goodsFilter,'wap');
        //$_return = array_values($_return['goodsRows']);
        break;

    case 'select':

        $goodsFilter['goods_id'] = explode(",", $setting["goods_select"]);
        $goodsFilter['goodsNum'] = $setting['limit'];
        $_return = b2c_widgets::load('Goods')->getGoodsList($goodsFilter,'wap');
        foreach (json_decode($setting['goods_select_linkobj'],1) as $obj) {
            if(trim($obj['pic'])!=""){
                $_return['goodsRows'][$obj['id']]['goodsPicL'] =
                    $_return['goodsRows'][$obj['id']]['goodsPicM'] =
                    $_return['goodsRows'][$obj['id']]['goodsPicS'] = $obj['pic'];
            }
            if(trim($obj['nice'])!=""){
                $_return['goodsRows'][$obj['id']]['goodsName'] = $obj['nice'];
            }
        }
        break;

    }
    $gids = array_keys($_return['goodsRows']);

    //商品标签
    if(is_array($_return['goodsRows'])){
        foreach($_return['goodsRows'] as $apk=>$apv){
            $_return['goodsRows'][$apk]['goods_id'] = $apv['goodsId'];
        }
    }
    foreach( kernel::servicelist('tags_special.add') as $services ) {
        if ( is_object($services)) {
            if ( method_exists( $services, 'add') ) {
                $services->add( $gids, $_return['goodsRows'] );
            }
        }
    }

    if(!$gids||count($gids)<1)return $_return;
    $mdl_product = app::get('b2c')->model('products');
    $products = $mdl_product ->getList('product_id, spec_info, price, freez, store, marketable, goods_id',array('goods_id'=>$gids,'marketable'=>'true'));
     // @zeng 2017.4.25 当前用户id
    $memberMolde=kernel::single('b2c_user_object');
    $member_id=$memberMolde->get_member_id();

    $mCartObject = app::get('b2c')->model('cart_objects');
    $goodsMdl = app::get('b2c')->model('goods');

    if($member_id)
    {
        $member_state=$memberMolde->get_member_state($member_id);
        $member_lv_id=$memberMolde->get_member_lv($member_id);
    }
    else
    {
        $member_state='-1';
        $member_lv_id='';
    }
    $data['info']=array('member_state'=>$member_state);
    //@zeng 2017.4.25 商品会员价
    $mlvpriceModel=kernel::single('b2c_goods_object');
  
    foreach ($products as $key => $product) {
        //要货规格
        $goodsGet = $goodsMdl->getList('*',array("goods_id"=>$product['goods_id']));
        $_return['goodsRows'][$product['goods_id']]['goodStandard'] =$goodsGet[0]['good_standard'];
       
        //插入购物车的数量quantity
        $goodsData1 = $mCartObject->getRow('quantity',array("member_id"=>$member_id,"obj_ident"=>'goods_'.$product['goods_id'].'_'.$product['goods_id']));
         $_return['goodsRows'][$product['goods_id']]['quantity'] = $goodsData1['quantity'];
        //在挂件中插入goods_id
         $_return['goodsRows'][$product['goods_id']]['goods_id'] = $product['goods_id'];

        $_return['goodsRows'][$product['goods_id']]['products'][] = $product;
        //@zng 2017.4.25 商品会员价
        $_return['goodsRows'][$product['goods_id']]['products'][] = $data;

        //用户审核状态
        $mlv_price=$mlvpriceModel->get_member_lv_goods_price($product['goods_id'],$member_lv_id);

        if($mlv_price)
            $_return['goodsRows'][$product['goods_id']]['products'][] = array('mlv_price'=>$mlv_price);//会员价
          
        else
             $_return['goodsRows'][$product['goods_id']]['products'][] = array('mlv_price'=>$product['price']);//销售价

         //@mo add 2017年5月2日 14:50:55
         $member_id = kernel::single('b2c_user_object')->get_member_id();
         if(!empty($member_id))
         {
             $now_can_buy_this_goods_quantity = 0;
               $now_can_buy_this_goods_quantity = kernel::single('b2c_checkproduct_check')->can_buy_productid_quantity($product['product_id']);
                
                
                 // 将小于触发值的数量提示去掉 add by hzk
               $getstartgiftnum = kernel::single('b2c_checkproduct_check')->getstartgiftnum('product_id',$product['product_id']);
                        
            if($now_can_buy_this_goods_quantity < $getstartgiftnum){
                $now_can_buy_this_goods_quantity = 999999;
              }
              if( $now_can_buy_this_goods_quantity!= -1 )  //@mo
              {
                $_return['goodsRows'][$product['goods_id']]['products'][] = array('type'=>app::get('b2c')->_('havegift'));
                $_return['goodsRows'][$product['goods_id']]['products'][] = array('maxbuy'=>$now_can_buy_this_goods_quantity);
              }
        } 
    }
     //更改首页商品促销的价格
   foreach ($_return['goodsRows'] as $key => $variable) {
            foreach ($variable['current_promotion']['goods'] as $key1 => $value) {
              if ($value['use'] == 'true') {
                //符合应用条件的商品以固定折扣出售(首批打折)
                 if ( $value['action_solution']['b2c_promotion_solutions_topercent2']) {
                 $_return['goodsRows'][$key]['mlv_price1'] = $variable['products'][2]['mlv_price']*$value['action_solution']['b2c_promotion_solutions_topercent2']['percent']/100;
                 $_return['goodsRows'][$key]['price1'] = $variable['products'][0]['price']*$value['action_solution']['b2c_promotion_solutions_topercent2']['percent']/100;
                //符合应用条件的商品以固定折扣出售
                 }else if($value['action_solution']['b2c_promotion_solutions_topercent']) {
                  $_return['goodsRows'][$key]['mlv_price1'] = $variable['products'][2]['mlv_price']*$value['action_solution']['b2c_promotion_solutions_topercent']['percent']/100;
                   $_return['goodsRows'][$key]['price1'] = $variable['products'][0]['price']*$value['action_solution']['b2c_promotion_solutions_topercent']['percent']/100;
                  //符合应用条件的商品固定价格购买
                 }else if($value['action_solution']['b2c_promotion_solutions_tofixed']) {
                  $_return['goodsRows'][$key]['mlv_price1'] = $value['action_solution']['b2c_promotion_solutions_tofixed']['total_amount'];
                  $_return['goodsRows'][$key]['price1'] = $value['action_solution']['b2c_promotion_solutions_tofixed']['total_amount'];
                  //符合应用条件的商品减去固定折扣出售
                 }else if($value['action_solution']['b2c_promotion_solutions_bypercent']) {
                  $_return['goodsRows'][$key]['mlv_price1'] =  $variable['products'][2]['mlv_price']*(1-$value['action_solution']['b2c_promotion_solutions_bypercent']['percent']/100);
                 $_return['goodsRows'][$key]['price1'] =  $variable['products'][0]['price']*(1-$value['action_solution']['b2c_promotion_solutions_bypercent']['percent']/100);
                  //符合应用条件的商品减固定价格购买
                 }else if($value['action_solution']['b2c_promotion_solutions_byfixed']) {
                  $_return['goodsRows'][$key]['mlv_price1'] = $variable['products'][2]['mlv_price']-$value['action_solution']['b2c_promotion_solutions_byfixed']['total_amount'];
                  $_return['goodsRows'][$key]['price1'] = $variable['products'][0]['price']-$value['action_solution']['b2c_promotion_solutions_byfixed']['total_amount'];
                 //符合应用条件的商品以固定价格出售【首批减价】【商品促销】
                  }else if($value['action_solution']['b2c_promotion_solutions_fltofixed']){
                  $_return['goodsRows'][$key]['mlv_price1']  = $value['action_solution']['b2c_promotion_solutions_fltofixed']['total_amount'];
                  $_return['goodsRows'][$key]['price1'] = $value['action_solution']['b2c_promotion_solutions_fltofixed']['total_amount'];
                 }
              }
            }
        }
        //更改首页订单促销的价格
        foreach ($_return['goodsRows'] as $key => $variable) {
             foreach ($variable['products']['current_promotion']['order'] as $key1 => $value) {
              if ($value['use'] == 'true') {
                //符合应用条件的商品以固定折扣出售(首批打折)
                 if ( $value['action_solution']['b2c_promotion_solutions_topercent2']) {
                 $_return['goodsRows'][$key]['mlv_price2'] = $variable['products'][2]['mlv_price']*$value['action_solution']['b2c_promotion_solutions_topercent2']['percent']/100;
                 $_return['goodsRows'][$key]['price2'] = $variable['products'][0]['price']*$value['action_solution']['b2c_promotion_solutions_topercent2']['percent']/100;
                //符合应用条件的商品以固定折扣出售
                 }else if($value['action_solution']['b2c_promotion_solutions_topercent']) {
                  $_return['goodsRows'][$key]['mlv_price2'] = $variable['products'][2]['mlv_price']*$value['action_solution']['b2c_promotion_solutions_topercent']['percent']/100;
                   $_return['goodsRows'][$key]['price2'] = $variable['products'][0]['price']*$value['action_solution']['b2c_promotion_solutions_topercent']['percent']/100;
                  //符合应用条件的商品固定价格购买
                 }else if($value['action_solution']['b2c_promotion_solutions_tofixed']) {
                  $_return['goodsRows'][$key]['mlv_price2'] = $value['action_solution']['b2c_promotion_solutions_tofixed']['total_amount'];
                  $_return['goodsRows'][$key]['price2'] = $value['action_solution']['b2c_promotion_solutions_tofixed']['total_amount'];
                  //符合应用条件的商品减去固定折扣出售
                 }else if($value['action_solution']['b2c_promotion_solutions_bypercent']) {
                  $_return['goodsRows'][$key]['mlv_price2'] =  $variable['products'][2]['mlv_price']*(1-$value['action_solution']['b2c_promotion_solutions_bypercent']['percent']/100);
                 $_return['goodsRows'][$key]['price2'] =  $variable['products'][0]['price']*(1-$value['action_solution']['b2c_promotion_solutions_bypercent']['percent']/100);
                  //符合应用条件的商品减固定价格购买
                 }else if($value['action_solution']['b2c_promotion_solutions_byfixed']) {
                  $_return['goodsRows'][$key]['mlv_price2'] = $variable['products'][2]['mlv_price']-$value['action_solution']['b2c_promotion_solutions_byfixed']['total_amount'];
                  $_return['goodsRows'][$key]['price2'] = $variable['products'][0]['price']-$value['action_solution']['b2c_promotion_solutions_byfixed']['total_amount'];
                  //没有促销以供货价出售
                 }else{
                    $_return['goodsRows'][$key]['mlv_price2'] = $variable['products'][2]['mlv_price'];
                   $_return['goodsRows'][$key]['price2'] = $variable['products'][0]['price'];
                 }
              }
            }
        }
       $add['isused'] = app::get('b2c')->getConf('system.member.IsUsed');
    
       $add['pernum'] = app::get('b2c')->getConf('system.product.per.num');
       
       $_return['add'] = $add;
       //add 这里是验证登录状态----未登录不显示商品价格 ---黄智坤06-19
        $userObject = kernel::single('b2c_user_object');
        if( $userObject->is_login()){
            $_return['check_login'] = 'login';
        }else{
            $_return['check_login'] = 'nologin';
        }
        //end
        // echo "<pre>";
        // var_dump($_return);
        // exit;
    return $_return;
}
?>

